package cn.edu.ctbu.labtest.controller;

import cn.edu.ctbu.labtest.domain.Course;
import cn.edu.ctbu.labtest.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@Slf4j
public class CourseController {

    @Autowired
    CourseService courseService;

    @RequestMapping("/course/findAll")
    public List<Course> findAll()
    {
        return courseService.findAll();
    }

    @RequestMapping("/course/clist")
    public String list(HttpServletRequest httprequest, Model model, String name){
        HttpSession session=httprequest.getSession();
        if(session.getAttribute("userid")==null){
            //未登录，需要去登录
            return "redirect:/login/user";
        }
        log.info("name:{}",name);
        List<Course> courses= courseService.findAll();
        model.addAttribute("data",courses);

        List<Course> courses1=courseService.findByName(name);
        model.addAttribute("courses1",courses1);

        return "/course/clist";
    }
}
